<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >

    <el-form-item label="设施类型" prop="deviceType">
        <el-select
          v-model="queryParams.deviceType"
          placeholder="请选择设施类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.DEVICE_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="路段类型" prop="roadType">
        <el-select
          v-model="queryParams.roadType"
          placeholder="请选择路段类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.ROAD_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="杆件编号" prop="memCode">
        <el-input
          v-model="queryParams.memCode"
          placeholder="请输入杆件编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="子编号" prop="subCode">
        <el-input
          v-model="queryParams.subCode"
          placeholder="请输入子编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="完整编号" prop="deviceCode">
        <el-input
          v-model="queryParams.deviceCode"
          placeholder="请输入完整编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      

      <el-form-item label="路口名" prop="roadName">
        <el-input
          v-model="queryParams.roadName"
          placeholder="请输入路口名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="一机一档" prop="distCode">
        <el-input
          v-model="queryParams.distCode"
          placeholder="请输入一机一档"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>

      
     
     
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['device:data:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['device:data:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap> 
  <!-- 列表 -->


  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号id" align="center" prop="id" v-if="false"/>
      <el-table-column label="设施类型" align="center" prop="deviceType"  >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.DEVICE_TYPE" :value="scope.row.deviceType" />
        </template>
      </el-table-column>
      <el-table-column label="路段类型" align="center" prop="roadType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ROAD_TYPE" :value="scope.row.roadType" />
        </template>
      </el-table-column>
      <el-table-column label="杆件编号" align="center" prop="memCode"  width="80px" />
     
      <el-table-column label="子编号" align="center" prop="subCode" width="80px" />
      <el-table-column label="完整编号" align="center" prop="deviceCode" width="100px" />

      <el-table-column label="路口名" align="center" prop="roadName" width="180px" />
      <el-table-column label="设备名称" align="center" prop="deviceName" width="180px"/>
      <el-table-column label="一机一档" align="center" prop="distCode" />
      <el-table-column label="纬度" align="center" prop="lat" width="80px" />
      <el-table-column label="经度" align="center" prop="lng" width="80px" /> 
      <el-table-column label="条形码" align="center" prop="deviceCode"  width="150px"  >
        <template #default="scope">
          <barcode-gen :value="scope.row.deviceCode"  :index="scope.row.deviceCode"/> 
        </template>
      </el-table-column>

      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="170px"
      /> 
     
      
      <el-table-column label="操作" align="center" min-width="100px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="preview( scope.row)" 
          >
            打印
          </el-button>

          <el-button
            link
            type="primary"
            @click="migrate( scope.row.id)" 
          >
            迁移
          </el-button>

<BR/>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['device:data:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['device:data:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <DataForm ref="formRef" @success="getList" />

  
  <PrintVue :addVisible="printVisible" ref="printRef" :info="printinfo" @closeprint="Closeprint"/>

</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { DataApi, DataVO } from '@/api/device/devicedata'
import DataForm from './DataForm.vue'
 
import BarcodeGen from './BarcodeGen.vue'
import PrintVue from './Print.vue'

const printVisible = ref(false);
const printinfo = ref(); 


defineComponent({
  components: {
    BarcodeGen,
  },
})


/** 设备台账 列表 */
defineOptions({ name: 'DeviceData' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<DataVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  roadName: undefined,
  deviceName: undefined,
  distCode: undefined,
  createTime: [],
  deviceCode: undefined,
  subCode: undefined,
  deviceType: undefined,
  roadType: undefined,
  memCode: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DataApi.getDataPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

//迁移
const migrate = (type: string, id?: number) => {
  //formRef.value.open(type, id)
}

const printRef=ref();
const preview=(row:Object)=> { 
  printinfo.value = row;
  printVisible.value = true;
  printRef.value.openpreview(row)
};
 
const Closeprint = () => {
  printVisible.value = false;
}; 


/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DataApi.deleteData(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await DataApi.exportData(queryParams)
    download.excel(data, '设备台账.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>